package com.lemurian.bankdata.bean;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.lemurian.bankdata.bean.base.BaseDaoHibernate;
import com.lemurian.bankdata.lite.logger.BankDataLogger;
import com.lemurian.entity.Category;
import com.lemurian.entity.Groups;
import com.lemurian.entity.Menu;

@Repository
public class GroupsDAO extends BaseDaoHibernate<Groups>{
	
	private BankDataLogger log = BankDataLogger.getLogger(GroupsDAO.class.getName());

	public Groups getGroup(int id) {
		Groups groups = null;

		try {
			
			Criteria criteria = getSession().createCriteria(Groups.class);
			
			criteria.setCacheable(Boolean.TRUE);
			
			criteria.add(Restrictions.eq("id", id));
			
			criteria.setFetchMode("menus", FetchMode.JOIN)
					.setCacheRegion(Menu.class.getName());
			
			criteria.createCriteria("groupType", "groupType", Criteria.LEFT_JOIN)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.setCacheRegion(Category.class.getName());
			
			criteria.addOrder(Order.asc("groupName"));

			groups = (Groups) criteria.uniqueResult();
		} catch (Exception e) {
            e.printStackTrace();
			getSession().getTransaction().rollback();
		}
		return groups;
	}
}